﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DocFactoryWinLibrary;

namespace WinClientExample
{
    public partial class frmMain : Form
    {
        private string mstrSavePath = null;
        private string mstrTemplateFilePath = null;

        public frmMain()
        {
            InitializeComponent();
        }

        private void butSelectSavePath_Click(object sender, EventArgs e)
        {
            fbDialog.ShowDialog();

            if (fbDialog.SelectedPath != "")
            {
                mstrSavePath = fbDialog.SelectedPath;
                lblSavePath.Text = mstrSavePath;
            }
        }

        private void butSelectTemplate_Click(object sender, EventArgs e)
        {
            ofDialog.ShowDialog();

            if (ofDialog.FileName != "")
            {
                mstrTemplateFilePath = ofDialog.FileName;
                lblTemplateFileName.Text = mstrTemplateFilePath;
            }
        }

        private void butGenerate_Click(object sender, EventArgs e)
        {
            if ((mstrTemplateFilePath != null) && (mstrTemplateFilePath != "")
                && (mstrSavePath != null) && (mstrSavePath != ""))
            {
                WinTemplateParser objTemplateParser = new WinTemplateParser();
                string strFileName = "";

                objTemplateParser.OpenFromTemplate(mstrTemplateFilePath, mstrSavePath, out strFileName );

                if(chkOpenWord.Checked)
                    XmlDocumentConverter.ConvertXMLToDoc(mstrSavePath, strFileName, strFileName.Replace("xml", "doc"), true);
            }
            else
            {
                MessageBox.Show("You must specify both the Template and Save path", "Unable to Generate");
            }
        }

        private void txtFileName_Leave(object sender, EventArgs e)
        {
            if (txtFileName.Text != "")
                if (txtFileName.Text.Contains("."))
                {
                    string[] strParts =  txtFileName.Text.Split('.');
                    if (strParts.Length > 2)
                        txtFileName.Text = strParts[0];
                    else
                    {
                        MessageBox.Show("Invalid File Name", "File Name Entered");
                        txtFileName.Text = "";
                    }
                }
        }
    }
}
